package com.jinqing.wuchuang.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.jinqing.wuchuang.entity.SettleCompany;
import com.jinqing.wuchuang.entity.SettledLegalRepresentative;
import com.jinqing.wuchuang.entity.SettledTeamMenber;
import com.jinqing.wuchuang.response.RespBean;
import com.jinqing.wuchuang.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 用于入住模块
 */
@RestController
@Slf4j
@RequestMapping("settle")
public class SettleController {

    @Autowired
    private SettledLegalRepresentativeService settledLegalRepresentativeService;
    @Autowired
    private SettleCompanyService settleCompanyService;
    @Autowired
    private AuthUserService authUserService;
    @Autowired
    private WordService wordService;
    @Autowired
    private SettleFileService settleFileService;

    /**
     * 更新法人的信息，一个user对应一个法人信息
     * @param settledLegalRepresentative
     * @return
     */
    @PostMapping("updateinformation")
    public RespBean updateRepresentativeInformation(SettledLegalRepresentative settledLegalRepresentative) {
        settledLegalRepresentativeService.updateRepresentativeInformation(settledLegalRepresentative);
        return RespBean.ok("success");
    }

    /**
     *
     * @param settledTeamMenbers
     * @return
     */
    @PostMapping("updateTeamMenber")
    public RespBean updateTeamMenber(@RequestBody List<SettledTeamMenber> settledTeamMenbers) {
        //检查所有参数的公司id是否相同
        int companyId = settledTeamMenbers.get(0).getCompanyId();
        for (SettledTeamMenber s : settledTeamMenbers) {
            if (!s.getCompanyId().equals(companyId)) {
                return RespBean.error("参数不合法");
            }
        }
        settleCompanyService.updateTeammenber(settledTeamMenbers);
        return RespBean.ok("success");

    }

    /**
     * 查看当前用户的法人信息
     *
     * @return
     */
    @GetMapping("getLegalRepresentatibeInformation")
    public RespBean getLegalRepresentatibeInformation() {
        return RespBean.ok("success", settledLegalRepresentativeService.getSettledLegalRepresentative());
    }

    /**
     * 查看当前法人是否完善信息
     *
     * @return
     */
    @GetMapping("isImproveInformation")
    public RespBean isImproveInformation() {
        return RespBean.ok("完善信息", settledLegalRepresentativeService.isImproveInformation());
    }

    /**
     * 增加新的申请,或修改，根据传入的settlecompany是否含有id字段
     * 先判断是否有id 然后调用不同的service就好了
     * 新建id为0就行
     *
     * @param settleCompany
     * @return
     */
    @GetMapping("updateCompanyInformation")
    public RespBean updateCompanyInformation(SettleCompany settleCompany) {

        if (settleCompany.getId() == 0) {
            //说明是新建的公司,先获取当前用户的uid
            settleCompany.setUid(authUserService.getUidByToken());
            settleCompanyService.insertCompany(settleCompany);
            return RespBean.ok("success", settleCompany);
        } else {
            //说明是以前暂存的更新就好
            //获取法人的所有公司，看发送的参数是否合法
            ArrayList<SettleCompany> settleCompanies = (ArrayList<SettleCompany>) settleCompanyService.getAllCompany();
            for(SettleCompany s:settleCompanies){
                if(s.getId()==settleCompany.getId()){
                    settleCompanyService.updateCompany(settleCompany);
                    return RespBean.ok("success", settleCompany);
                }
            }
           return RespBean.error("公司不存在");
        }
    }

    /**
     * 查看当前法人所有的公司
     */
    @GetMapping("getAllCompany")
    public RespBean getAllCompany() {
        return RespBean.ok("success", settleCompanyService.getAllCompany());
    }

    /**
     * 根据公司id查看详情信息
     *
     * @return
     */
    @GetMapping("getCompanyByCompanyId")
    public RespBean getCompanyByCompanyId(@Param("id") Integer id) {
        ArrayList<SettleCompany> companies = (ArrayList<SettleCompany>) settleCompanyService.getAllCompany();
        for (SettleCompany s : companies) {
            if (s.getId().equals(id)) {
                return RespBean.ok("success", settleCompanyService.getCompanyById(id));
            }
        }
        return RespBean.error("权限错误，不允许访问");
    }


    /**
     * 根据公司id获得word
     *
     * @param companyId
     * @return 返回网址
     */
    @GetMapping("creatWord")
    public RespBean getWord(@RequestParam("companyId") int companyId) {
        //先判断参数合理性，查出当前用户所有拥有的公司
        ArrayList<SettleCompany> settleCompanies = (ArrayList<SettleCompany>) settleCompanyService.getAllCompany();
        for (SettleCompany c:settleCompanies) {
            if (c.getId().equals(companyId)) {
                String result = null;
                try {
                    result = wordService.creatWordURL(authUserService.getUidByToken(), companyId);
                    log.info(result);
                } catch (IOException e) {
                    log.error("发生错误",e);
                }
                return RespBean.ok(result);
            }
        }
        return RespBean.error("无权访问");
    }

    /**
     * 根据公司id获得word
     *
     * @param companyId
     * @return 返回流
     */
    @GetMapping("createWord")
    public RespBean getWordById(@RequestParam("companyId") int companyId,HttpServletResponse response) {
        //先判断参数合理性，查出当前用户所有拥有的公司
        ArrayList<SettleCompany> settleCompanies = (ArrayList<SettleCompany>) settleCompanyService.getAllCompany();
        for (SettleCompany c:settleCompanies) {
            if (c.getId().equals(companyId)) {
                String result = null;
                try {
                    result = wordService.creatWordById(response,authUserService.getUidByToken(), companyId);
                    log.info(result);
                } catch (IOException e) {
                    log.error("发生错误",e);
                }
                return RespBean.ok(result);
            }
        }
        return RespBean.error("无权访问");
    }

    /**
     * 根据公司id 获取所有的用户
     *
     * @param companyId
     * @return
     */
    @GetMapping("getAllMenberByCompanyId")
    public RespBean getAllMenberByCompanyId(Integer companyId) {
        ArrayList<SettleCompany> settleCompanies = (ArrayList<SettleCompany>) settleCompanyService.getAllCompany();
        for (SettleCompany c : settleCompanies) {
            if (c.getId().equals(companyId)) {
                return RespBean.ok("success", settleCompanyService.getAllSettledTeamMenbers(companyId));
            }
        }
        return RespBean.error("无权访问");
    }
    /**
     * 上传附件
     */
    @PostMapping("upAttachment")
    public RespBean upAttachment(Integer companyId, MultipartFile file){
        try {
            settleFileService.upAttachment(companyId, file);
        } catch (IOException e) {
            return RespBean.error("error");
        }
        return RespBean.ok("success");
    }
    /**
     * 查看公司名下附件
     */
    /**
     * 上传财务报表
     */
}

